var express = require('express');
var router = express.Router();

var { selectAllGoods, delGoods, insertGoods } = require("../db/good.js")

router.get('/', function (req, res, next) {
    res.render('index', { title: 'Express' });
});

router.post('/allGoods', function (req, res, next) {
    let body = req.body;
    // console.log(body)
    let data = {}
    // let { aTypeId, bTypeId, goodName, page, num } = body
    // if (!aTypeId) aTypeId = "";
    // if (!bTypeId) bTypeId = "";
    // if (!goodName) goodName = "";
    // if (!page) page = 1;
    // if (!num) num = 15;
    // let obj = { aTypeId, bTypeId, goodName, page, num }
    selectAllGoods(body).then(e => {
        if (e) {
            data.message = "获取成功"
            data.code = "0"
            data.data = e.rows
            data.sum = e.sum
        } else {
            data.message = "获取失败"
            data.code = "1"
        }
        res.send(data)
    }).catch(e => {
        data.message = "系统出错"
        data.code = "err"
        data.data = e
        res.send(data)
    })
})

router.post('/delGoods', function (req, res, next) {
    let { ids } = req.body;
    let data = {}
    delGoods(ids).then(e => {
        if (e) {
            data.message = "删除成功"
            data.code = "0"
        } else {
            data.message = "删除失败"
            data.code = "1"
        }
        res.send(data)
    }).catch(e => {
        data.message = "系统出错"
        data.code = "err"
        data.data = e
        res.send(data)
    })
})

router.post('/addGoods', function (req, res, next) {
    let body = req.body;
    console.log(body)
    let data = {}
    insertGoods(body).then(e => {
        if (e) {
            data.message = "新增成功"
            data.code = "0"
        } else {
            data.message = "新增失败"
            data.code = "1"
        }
        res.send(data)
    }).catch(e => {
        data.message = "系统出错"
        data.code = "err"
        data.data = e
        res.send(data)
    })
})


module.exports = router;
